package tt.dz.appserver.mvc.manage;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import tt.dz.appserver.entity.model.ResultMappingEntity;
import tt.dz.appserver.entity.model.pub.Label;
import tt.dz.appserver.service.business.LabelService;
import tt.dz.appserver.util.Result;
import tt.dz.appserver.util.enums.Response;

/**
 * 标签管理
 * @author USER001
 *
 */
@RestController
@RequestMapping("/manage/label")
public class LabelManager {

	/**标签管理*/
	@Resource
	LabelService pubLabelService;
	
	/**
	 * 标签增加
	 **@param labelid		标签号
	 * @param labeltype		标签类型	
	 * @param labelname		标签名称
	 * @return
	 */	
	@RequestMapping(value="/PubLabelAdd",produces={"application/json;charset=utf-8"})

	public String publabeladd(
		 	@RequestParam(value="labeltype") Integer labeltype,
		 	@RequestParam(value="labelname") String labelname) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubLabelService.publabeladd(labeltype, labelname);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 标签删除
	 * @param labelid		标签号
	 **@param labeltype		标签类型	
	 **@param labelname		标签名称
	 * @return
	 */	
	@RequestMapping(value="/PubLabelDel",produces={"application/json;charset=utf-8"})

	public String publabeldel(
		 	@RequestParam(value="labelid") String labelid) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubLabelService.publabeldel(labelid);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 标签更新
	 * @param labelid		标签号
	 * @param labeltype		标签类型	
	 * @param labelname		标签名称
	 * @return
	 */	
	@RequestMapping(value="/PubLabelUpdate",produces={"application/json;charset=utf-8"})

	public String publabelupdate(
			@RequestParam(value="labelid") String labelid,
			@RequestParam(value="labeltype", required=false) Integer labeltype,
		 	@RequestParam(value="labelname", required=false) String labelname) {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			JSONObject resultObject = pubLabelService.publabelupdate(labelid, labeltype, labelname);
			
			String code = resultObject.getString("code");
			String msg = resultObject.getString("msg");
			
			if (Response.SUCCESS.getCode().equals(code)) {
				//返回参数拼接（删除resultObject对象中的code和msg再返回）
				resultObject.remove("code");
				resultObject.remove("msg");
				
				resultMappingEntity = Result.responseBody(code, msg, resultObject);
			}
			else {
				resultMappingEntity = Result.responseBody(code, msg);
			}			
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
	
	/**
	 * 标签查找
	 **@param labelid		标签号
	 **@param labeltype		标签类型	
	 **@param labelname		标签名称
	 * @return
	 */	
	@RequestMapping(value="/PubLabelSelect",produces={"application/json;charset=utf-8"})

	public String publabelselect() {
		//返回数据映射实体
		ResultMappingEntity resultMappingEntity = null;
		
		try {
			
			//添加反馈
			List<Label> resultObject = pubLabelService.publabelselect();
			
			String code = Response.SUCCESS.getCode();
			String msg = Response.SUCCESS.getMsg();
			
			resultMappingEntity = Result.responseBody(code, msg, resultObject);
			
		} catch(Exception e) {
			e.printStackTrace();
			//异常
			resultMappingEntity = Result.responseBody(Response.RUNTIME_ERROR.getCode(), Response.RUNTIME_ERROR.getMsg(), e.getMessage());
		}
		return resultMappingEntity.toString();
	}
}
